<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 银商通
 */
class YinshangtongController extends PayController{
    protected $config;

    public function pay($array){
        $this->config = M("Channel")->where(array('id'=>$array['channel']))->find();

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'013']);
        }

        $product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        //echo '<pre/>'; print_r($product);exit;
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'013']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['order'] = $return['orderid'];
        $data['transtime'] = getMillisecond();
        $data['amount'] = intval($return['amount'] * 100);
        $data['productcategory'] = 1;
        $data['productname'] = 'zf';
        $data['productdesc'] = 'zf';
        $data['productprice'] = $data['amount'];
        $data['productcount'] = 1;
        $data['userip'] = $_SERVER['REMOTE_ADDR'];
        $data['areturl'] = $return['notifyurl'];
        $data['sreturl'] = $return['callbackurl'];

        switch($product['paytype']){
            //支付宝扫码
            case 3:
                $data['pnc'] = '88002';
                break;
            //微信扫码
            case 1:
                $data['pnc'] = '87028';
                break;
                //支付宝H5
            case 4:
                $data['pnc'] = '87023';
            break;
            //网银直连
            case 6:
                switch($product['code']){
                    //北京银行
                    case 'BOB':
                        $data['pnc'] = '87015';
                        break;
                    //东亚银行
                    case 'BEA':
                        $data['pnc'] = '87019';
                        break;
                    //中国工商银行
                    case 'ICBC':
                        $data['pnc'] = '87003';
                        break;
                    //光大银行
                    case 'CEB':
                        $data['pnc'] = '87012';
                        break;
                    //广发银行
                    case 'CGB':
                        $data['pnc'] = '87010';
                        break;
                    //华夏银行
                    case 'HXB':
                        $data['pnc'] = '87020';
                        break;
                    //中国建设银行
                    case 'CCB':
                        $data['pnc'] = '87004';
                        break;
                    //交通银行
                    case 'BOCOM':
                        $data['pnc'] = '87005';
                        break;
                    //中国民生银行
                    case 'CMBC':
                        $data['pnc'] = '87007';
                        break;
                    //南京银行
                    case 'NJCB':
                        $data['pnc'] = '87016';
                        break;
                    //宁波银行
                    case 'NBCB':
                        $data['pnc'] = '87017';
                        break;
                    //中国农业银行
                    case 'ABC':
                        $data['pnc'] = '87002';
                        break;
                    //平安银行
                    case 'PAB':
                        $data['pnc'] = '87014';
                        break;
                    //上海银行
                    case 'SHB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'013']);
                        break;
                    //上海浦东发展银行
                    case 'SPDB':
                        $data['pnc'] = '87009';
                        break;
                    //深圳发展银行
                    case 'SDB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'013']);
                        break;
                    //兴业银行
                    case 'CIB':
                        $data['pnc'] = '87008';
                        break;
                    //中国邮政储蓄银行
                    case 'PSBS':
                        $data['pnc'] = '87013';
                        break;
                    //招商银行
                    case 'CMB':
                        $data['pnc'] = '87006';
                        break;
                    //浙商银行
                    case 'CZB':
                        $this->showmessage('暂不支持此银行卡!',['code'=>'013']);
                        break;
                    //中国银行
                    case 'BOC':
                        $data['pnc'] = '87001';
                        break;
                    //中信银行
                    case 'CTTIC':
                        $data['pnc'] = '87011';
                        break;
                    default:
                        $this->showmessage('暂不支持此银行卡!',['code'=>'013']);
                }
                break;
            default:
                $this->showmessage('暂不支持此支付方式!',['code'=>'013']);
        }

        ksort($data);

        $str = "";
        foreach($data as $k=>$v){
            if(!$v){
                continue;
            }
            $str .= $v;
        }
        $str .= $this->config['signkey'];
        $data['sign'] = md5($str);

        $d['merchantaccount'] = $this->config['mch_id'];//商户号
        $d['encryptkey'] = '1';//加密请求key
        $d['data'] = urlencode(json_encode($data));//加密请求数据

        $res = curl($this->config['gateway'], http_build_query($d));

        echo $res;
    }

    //通知
    public function notifyurl(){
        $p = file_get_contents('php://input');
        if(!$p){
            exit("");
        }
        $p = urldecode($p);
        parse_str($p, $p);
        if(!$p || empty($p)){
            exit("");
        }

        $data = json_decode($p['data'], true);
        if(!$data || empty($data)){
            exit("");
        }

        $sign = $data['sign'];
        unset($data['sign']);

        //获取订单信息
        $orderid = $data['order'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo ''; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> file_get_contents('php://input'),
            "pay_orderid_rd"=> $data['payorderid']
        ));
        Order::getInstance()->edit($find['id'],array("server_return"=>file_get_contents('php://input'), "pay_orderid_rd"=>$data['payorderid']));
        Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array("server_return"=>file_get_contents('php://input'), "pay_orderid_rd"=>$data['payorderid']));
        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Yinshangtong','title'=>$find['pay_yzh_tongdao']))->find();
        if(!$this->config){
            exit("");
        }

        ksort($data);
        $str = "";
        foreach($data as $k=>$v){
            if(!$v){
                continue;
            }
            $str .= $v;
        }
        $str .= $this->config['signkey'];
        $sign2 = md5($str);

        if($sign != $sign2){
            exit("");
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Yinshangtong",0);

        //回调成功
        echo 'SUCCESS'; exit;
    }

    public function callBackUrl(){
        echo '操作成功'; exit;
    }
}